package me.luck2.wicketcaptcha;

import org.apache.wicket.extensions.markup.html.captcha.CaptchaImageResource;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.value.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CaptchaPage extends WebPage {
	Logger logger = LoggerFactory.getLogger(CaptchaPage.class);

	private final class CaptchaForm<T> extends Form<T> {

		private final CaptchaImageResource captchaImageResource;

		/**
		 * Construct.
		 * 
		 * @param id
		 */
		public CaptchaForm(String id) {
			super(id);

			captchaImageResource = new CaptchaImageResource(imagePass);
			add(new Image("captchaImage", captchaImageResource));
			add(new RequiredTextField<String>("password", new PropertyModel<String>(properties, "password")) {
				@Override
				protected final void onComponentTag(final ComponentTag tag) {
					super.onComponentTag(tag);
					// clear the field after each render
					tag.put("value", "");
				}
			});
		}

		/**
		 * @see org.apache.wicket.markup.html.form.Form#onSubmit()
		 */
		@Override
		public void onSubmit() {
			if (!imagePass.equals(getPassword())) {
				error("Captcha password '" + getPassword() + "' is wrong.\n" + "Correct password was: " + imagePass);
			} else {
				info("Success!");
			}

			// force redrawing
			captchaImageResource.invalidate();
		}
	}

	private static int randomInt(int min, int max) {
		return (int) (Math.random() * (max - min) + min);
	}

	private static String randomString(int min, int max) {
		int num = randomInt(min, max);
		byte b[] = new byte[num];
		for (int i = 0; i < num; i++)
			b[i] = (byte) randomInt('a', 'z');
		return new String(b);
	}

	/** Random captcha password to match against. */
	private final String imagePass = randomString(4, 6);

	private final ValueMap properties = new ValueMap();

	/**
	 * Construct.
	 * 
	 * @param parameters
	 */
	public CaptchaPage(final PageParameters parameters) {
		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		add(feedback);
		add(new CaptchaForm<Void>("captchaForm"));
	}

	private String getPassword() {
		return properties.getString("password");
	}
}